package dobest.ray.com.jiongjiong.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import dobest.ray.com.jiongjiong.R;

/**
 * Created by Ray on 2016/3/15.
 * https://github.com/ray0807
 * 目明乐视中使用的柱状图
 */
public class BarChartView extends View {
    private Paint paintText;
    private Paint paintBar;
    private Paint paintBarText;
    private Context context;

    private float paddingBottom = 50.0f;
    private float paddingLeft = 40.0f;
    private float paddingRight = 40.0f;
    private float paddingTop = 50.0f;
    private float paddingBar = 30.0f;//柱状图之前的距离
    private float itemLength = 0;// 横坐标没格刻度长度
    private int perScreenBarNum = 8;//固定每屏几个柱状图
    private int barNum = 0;//8个柱状图
    private int timeTextHeight = 20;//日期高度
    private float base;// 每一格代表多少像素
    private int min = 0;
    private int max = -1;
    private List<Integer> barData = new ArrayList<>();

    private List<String> datesYear = new ArrayList<>();
    private List<String> datesMon = new ArrayList<>();

    public BarChartView(Context context,List<Integer> barData,List<String> datesYear,List<String> datesMon) {
        super(context);
        this.context = context;
        this.barData=barData;
        this.datesYear=datesYear;
        this.datesMon=datesMon;
        init();
    }


    public BarChartView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    public BarChartView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init();
    }

    private int getScreenWidth() {
        WindowManager wm = (WindowManager) getContext().getSystemService(
                Context.WINDOW_SERVICE);
        return wm.getDefaultDisplay().getWidth();
    }

    private void init() {
        initPaint();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        base = (getHeight() - paddingBottom - paddingTop - timeTextHeight) / (max - min);// 每一格刻度代表多高
        drawTextAndBar(canvas);
        super.onDraw(canvas);
    }

    private void drawTextAndBar(Canvas canvas) {
        Shader mShader = new LinearGradient(0, 0, 0, getHeight(), new int[]{
                Color.argb(255, 0, 185, 168), Color.argb(255, 0, 185, 168),
                Color.argb(255, 0, 185, 168)}, null, Shader.TileMode.CLAMP);
        paintBar.setShader(mShader);
        //draw bar
        Path path = new Path();
        for (int i = 0; i < barData.size(); i++) {
            float startX = paddingLeft + itemLength * i + i * paddingBar;
            float stopX = paddingLeft + itemLength * (i + 1) + i * paddingBar;
            float startY = (getHeight() - paddingBottom)
                    - (barData.get(i) - min) * base;
//            float stopY = (getHeight() - paddingBottom)
//                    - (barData.get(i + 1) - min) * base;
            path.lineTo(startX, getHeight() - paddingBottom);

            path.lineTo(startX, startY);
            path.lineTo(stopX, startY);
            path.lineTo(stopX, getHeight() - paddingBottom);
            path.lineTo(startX, getHeight() - paddingBottom);

            path.close();
            canvas.drawPath(path, paintBar);

            canvas.drawText(barData.get(i) + "回", startX + itemLength / 2 - paintBarText.measureText(barData.get(i) + "回") / 2, startY - 15, paintBarText);
            canvas.drawText(datesYear.get(i), startX + itemLength / 2 - paintText.measureText(datesYear.get(i)) / 2, getHeight() - paddingBottom + 20, paintText);
            canvas.drawText(datesMon.get(i), startX + itemLength / 2 - paintText.measureText(datesMon.get(i)) / 2, getHeight() - paddingBottom + 40, paintText);
        }

    }

    private void initPaint() {
        barNum = datesMon.size();
        max = Collections.max(barData);
//        min = Collections.min(barData);


        paintText = new Paint();
        paintText.setStrokeWidth(4f);
        paintText.setTextSize(20);
        paintText.setColor(getResources().getColor(R.color.grey));
        paintText.setAntiAlias(true);

        paintBar = new Paint();
        paintBar.setAntiAlias(true);
        paintBar.setStrokeWidth(2f);


        paintBarText = new Paint();
        paintBarText.setTextSize(26);
        paintBarText.setStrokeWidth(4f);
        paintBarText.setColor(getResources().getColor(R.color.colorPrimary));
        paintBarText.setAntiAlias(true);
        itemLength = (getScreenWidth() - paddingLeft - paddingRight - (perScreenBarNum - 1) * paddingBar) / (perScreenBarNum * 1.0f);
        setLayoutParams(new ViewGroup.LayoutParams((int) (itemLength * barNum + (barNum - 1) * paddingBar + paddingLeft + paddingRight), ViewGroup.LayoutParams.MATCH_PARENT));
    }

}
